package time;

import sun.util.resources.LocaleData;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.Locale;

/**
 * @author du.senlin
 * @Title:
 * @Description:
 * @date 2020/3/30 13:52
 */
public class Time8 {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate.getYear());
        System.out.println(localDate.getMonth());
        System.out.println(localDate.getDayOfMonth());
        // 创建特定日期
        LocalDate of = LocalDate.of(2020, 3, 31);
        System.out.println(of);
        // 比较日期是否相等
        System.out.println(localDate.equals(of));
        // 比较生日是否相等
        MonthDay birth = MonthDay.of(of.getMonth(),of.getDayOfMonth());
        MonthDay curr =MonthDay.from(localDate);
        if(birth.equals(curr)){
            System.out.println("今天是你哒生日，祝你生日快乐");
        }else {
            System.out.println("sorry");
        }
        // 获取指定N后的时间
        LocalTime localTime = LocalTime.now();
        System.out.println("2小时后："+localTime.plusHours(2));
        System.out.println("2分钟后："+localTime.plusMinutes(20));
        System.out.println("2秒后："+localTime.plusSeconds(20));
        System.out.println("2天后："+localDate.plusDays(2));
        System.out.println("2月后："+localDate.plusMonths(2));
        System.out.println("2年后："+localDate.plusYears(2));
        System.out.println("2年前："+localDate.plusYears(-2));
        System.out.println("1年后："+localDate.plus(1, ChronoUnit.YEARS));
        System.out.println("1年前："+localDate.minus(1, ChronoUnit.YEARS));
        System.out.println("1小时前："+localTime.minus(1, ChronoUnit.HOURS));
        System.out.println("1秒后："+localTime.plus(1, ChronoUnit.SECONDS));
        // 判断之前或者之后
        System.out.println(localDate+"是否在"+of+"之后："+localDate.isAfter(of));
        System.out.println(localDate+"是否在"+of+"之前："+localDate.isBefore(of));
        // 计算日期之间的差值
        System.out.println("相差多久"+Period.between(localDate,of));
        // 获取某个时区下（所以对时区是敏感的）当前的瞬时时间、日期
        Clock clock = Clock.systemUTC();
        System.out.println(clock);
        System.out.println(Clock.systemDefaultZone());
        System.out.println(Clock.system(ZoneId.systemDefault()));
        // 指定日期格式
        DateTimeFormatter dateTimeFormatter =  DateTimeFormatter.ofPattern("MM dd yyyy");
        String st = "09 15 2020";
        LocalDate localDate1 = LocalDate.parse(st,dateTimeFormatter);
        System.out.println(localDate1);
        long l = intervalDate("2020-09-09", "2020-09-12");
        System.out.println(l);

    }

    /**
     * 按照时间指定格式计算差值
     * @param startTime
     * @param endTime
     * @return
     */
    private static long intervalDate(String startTime, String endTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startLocal = LocalDate.parse(startTime, pattern);
        LocalDate endLoclal = LocalDate.parse(endTime, pattern);
        return startLocal.until(endLoclal, ChronoUnit.DAYS);
    }
}
